package com.lsz.flight_system.service.Impl;


import com.lsz.flight_system.entity.UserMessage;
import com.lsz.flight_system.repository.UserMessageRepository;
import com.lsz.flight_system.service.UserMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class UserMessageServiceImpl implements UserMessageService {

    @Autowired
    UserMessageRepository userMessageRepository;


    @Override
    public UserMessage getUserMessageByUsername(String username) {
        return userMessageRepository.findUserMessageByUsername(username);
    }

    @Override
    public boolean registerUserMessage(UserMessage userMessage) {
        String username = userMessage.getUsername();
        if (userMessageRepository.findUserMessageByUsername(username) != null) {
            return false;
        } else {
            userMessageRepository.save(userMessage);
            return true;
        }
    }

    @Override
    public boolean delUserMessageByUsername(String username) {
        UserMessage userMessage = userMessageRepository.findUserMessageByUsername(username);
        if (userMessage != null) {
            userMessageRepository.delete(userMessage);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean userRecharge(String username, float money) {
        UserMessage userMessage = userMessageRepository.findUserMessageByUsername(username);
        float balance = userMessage.getBalance() + money;
        userMessage.setBalance(balance);
        try {
            userMessageRepository.save(userMessage);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean userWithdraw(String username) {
        UserMessage userMessage = userMessageRepository.findUserMessageByUsername(username);
        userMessage.setBalance(0);
        userMessageRepository.save(userMessage);
        return true;
    }

    @Override
    public List<UserMessage> findAllUserMessage(Map<String, Object> map) {
        return userMessageRepository.findAll(new Specification<UserMessage>() {
            @Override
            public Predicate toPredicate(Root<UserMessage> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                if (map.containsKey("username") && !StringUtils.isEmpty(map.get("username"))){
                    predicateList
                            .add(cb.like(root.get("username"),"%"+map.get("username").toString()+"%"));
                }
                if (map.containsKey("phone") && !StringUtils.isEmpty(map.get("phone"))){
                    predicateList
                            .add(cb.equal(root.get("phone").as(String.class),map.get("phone")));
                }

                Predicate[] predicateArr = new Predicate[predicateList.size()];
                return cb.and(predicateList.toArray(predicateArr));
            }
        });
    }

    @Override
    public boolean saveUserMessage(UserMessage userMessage) {
        try {
            userMessageRepository.save(userMessage);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
